//
//  XMAreaSelectViewController.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/15.
//

#import "XMAreaSelectViewController.h"
#import "XMAreaSelectView.h"
#import "XMHAreaModel.h"

@interface XMAreaSelectViewController ()

@property (nonatomic, strong) XMAreaSelectView *areaSelectView;

@end

@implementation XMAreaSelectViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self hideNavigationBar:YES animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    [self.view addSubview:self.areaSelectView];
    self.title = self.areaStr;
    [_areaSelectView setContentWithAreaModelArr:self.areaModelArr];
}

- (XMAreaSelectView *)areaSelectView {
    if (!_areaSelectView) {
        _areaSelectView = [[XMAreaSelectView alloc] initWithFrame:self.view.bounds];
        
        XMWeakSelf;
        _areaSelectView.areaSelectBlock = ^(NSInteger selectIndex) {
            XMHAreaModel *areaModel = weakSelf.areaModelArr[selectIndex];
            if (areaModel.children.count > 0) {
                XMAreaSelectViewController *areaSelectVC = [[XMAreaSelectViewController alloc] init];
                areaSelectVC.areaStr = areaModel.name;
                if (weakSelf.selectAreaStr.length > 0) {
                    areaSelectVC.selectAreaStr = [NSString stringWithFormat:@"%@-%@", weakSelf.selectAreaStr, areaModel.name];
                }
                else {
                    areaSelectVC.selectAreaStr = areaModel.name;
                }
                NSLog(@"%@", weakSelf.selectAreaStr);
                areaSelectVC.areaModelArr = areaModel.children;
                areaSelectVC.selectBlock = ^(NSString * _Nonnull areaStr, NSString * _Nonnull areaId) {
                    weakSelf.selectBlock ? weakSelf.selectBlock(areaStr, areaId) : nil;
                };
                [weakSelf.navigationController pushViewController:areaSelectVC animated:YES];
            }
            else {
                NSString *selectAllStr = [NSString stringWithFormat:@"%@-%@", weakSelf.selectAreaStr, areaModel.name];
                weakSelf.selectBlock ? weakSelf.selectBlock(selectAllStr, areaModel.areaId) : nil;
                [weakSelf dismissViewControllerAnimated:YES completion:nil];
//                [weakSelf.navigationController popViewControllerAnimated:NO];
            }
        };
    }
    return _areaSelectView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
